//
//  CHComplainCountController.m
//  CarFile
//
//  Created by 陈浩 on 2017/4/12.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHComplainCountController.h"
#import "CHComplainCountCell.h"
#import "CHScreeningViewController.h"
#import "CHEvaluationListController.h"
#import "CHComplaintListController.h"
#import "PJ_Model.h"
#import "TS_Model.h"
@interface CHComplainCountController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) NSMutableArray *datas;
@property (nonatomic ,strong) UILabel *titleLabel;

@end

@implementation CHComplainCountController
-(NSMutableArray *)datas{
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
  
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.hidden = NO;
    
    //    设置右边UIBarButtonItem
    [self setupRightBarButtonItem];
    
    [self setupUI];
    
//    [super showLoading:YES];
    [BQActivityView showActiviTy];
    if ([self.title isEqualToString:@"评价统计"]) {
        
        [self getPJData];
        
    }else{
        
        [self getTSData];
    }
}




-(void)setupRightBarButtonItem{
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.bounds = CGRectMake(0, 0, 40 ,15);
    btn.titleLabel.font = Font(16);
    btn.titleLabel.textAlignment = NSTextAlignmentRight;
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setTitle:@"筛选" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(screeningData) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
}

//   筛选
-(void)screeningData{
    
    
    id obj = [LongriseRouter openURL:@"CHScreeningViewController"];
    if(obj && [obj isKindOfClass:[UIViewController class]])
    {
        UIViewController *vc = obj;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    //判断该类是否可以传参数
    if([LongriseRouter isImplLongriseRouterDelegaterWithObject:obj])
    {
        id<LongriseRouterDelegater> delegater = obj;
        if(delegater)
        {
            NSMutableDictionary *params = [NSMutableDictionary dictionary];
            if ([self.title isEqualToString:@"评价统计"]) {
                params[@"title"] = @"评价筛选";
            }else{
                params[@"title"] = @"投诉筛选";
            }
            params[@"starttime"] = self.startTime;
            params[@"endtime"] = self.endTime;
            //进行传值，传入统一的block以供B进行回调
            __weak typeof(self) weakSelf = self;
            [delegater setParams:params block:^(id obj) {
                
                if(obj && [obj isKindOfClass:[NSMutableDictionary class]])
                {
                    NSMutableDictionary *dict = obj;
                    weakSelf.areaid = dict[@"areaid"];
                    weakSelf.startTime = dict[@"startTime"];
                    weakSelf.endTime = dict[@"endTime"];
                    weakSelf.areacn = dict[@"areacn"];
                    if (weakSelf.areacn == nil) {
                        weakSelf.areacn = (NSString *)[Util getValue:@"areacn"];
                    }
                    
                    weakSelf.titleLabel.text = [NSString stringWithFormat:@"当前筛选条件: %@ %@——%@",weakSelf.areacn,weakSelf.startTime,weakSelf.endTime];
                    if ([self.title isEqualToString:@"评价统计"]) {
                        [weakSelf getPJData];
                    }else{
                        [weakSelf getTSData];
                    }
                }
                
            }];
        }
    }
}


-(void)getTSData{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (self.areaid) {
        params[@"areaid"] = self.areaid;
        params[@"sign"] = [DESCript encrypt:self.areaid encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    }else{
        params[@"areaid"] = [Util getValue:@"areaid"];
        params[@"sign"] = [DESCript encrypt:(NSString *)[Util getValue:@"areaid"] encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    }
    params[@"usertype"] = [Util getValue:@"usertype"];
    params[@"user"] = [Globle getInstance].user;
    params[@"start"] = self.startTime;
    params[@"end"] = self.endTime;

    [[Globle getInstance].service requestWithServiceName:@"lcipappgovecomplaintlistbyarea" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {

        [super showLoading:NO];
        [BQActivityView hideActiviTy];
        [self.datas removeAllObjects];
        if ([result[@"code"] isEqualToString:@"1"]) {
            for (NSDictionary *dict in result[@"data"][@"list"]) {
                
                TS_Model *model = [TS_Model objectWithKeyValues:dict];
                [self.datas addObject:model];
                
            }
        }
        [self.tableView reloadData];
        
    }];
    
}


-(void)getPJData{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (self.areaid) {
        params[@"areaid"] = self.areaid;
        params[@"sign"] = [DESCript encrypt:self.areaid encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
        
    }else{
        params[@"areaid"] = [Util getValue:@"areaid"];
        params[@"sign"] = [DESCript encrypt:(NSString *)[Util getValue:@"areaid"] encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    }
    params[@"usertype"] = [Util getValue:@"usertype"];
    params[@"user"] = [Globle getInstance].user;
    params[@"start"] = self.startTime;
    params[@"end"] = self.endTime;

    [[Globle getInstance].service requestWithServiceName:@"lcipappgovevaluatelistbyarea" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
        [super showLoading:NO];
        [BQActivityView hideActiviTy];
        [self.datas removeAllObjects];

        if ([result[@"code"] isEqualToString:@"1"]) {
            for (NSDictionary *dict in result[@"data"][@"list"]) {
                
                PJ_Model *model = [PJ_Model objectWithKeyValues:dict];
                [self.datas addObject:model];
                
            }
        
        }
         [self.tableView reloadData];
    }];
}


-(void)setupUI{
    NSArray *titles = nil;
    if ([self.title isEqualToString:@"投诉统计"]) {
        titles = @[@"区域",@"投诉总数",@"已处理",@"已督办",@"处理率"];
    }else{
        titles = @[@"区域",@"评价数",@"非常满意",@"满意",@"不满意",@"满意度"];
    }
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, UIScreenW -10, 30)];
    titleLabel.font = Font(12);
    titleLabel.textColor = [UIColor colorWithHexString:@"666666" alpha:1.0f];
    titleLabel.text = [NSString stringWithFormat:@"当前筛选条件: %@ %@——%@",self.areacn,self.startTime,self.endTime];
    [self.view addSubview:titleLabel];
    self.titleLabel = titleLabel;
    
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(titleLabel.frame), UIScreenW, 44*ScaleY)];
    headView.backgroundColor = [UIColor colorWithHexString:@"eeeef0" alpha:1.0f];
    [self.view addSubview:headView];
    CGFloat labelW = UIScreenW / titles.count;
    for (int i = 0; i < titles.count; i++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(i *labelW , 0, labelW, 44*ScaleY)];
        label.textColor = [UIColor colorWithHexString:@"148EC7" alpha:1.0f];
        label.font = Font(14);
        label.textAlignment = NSTextAlignmentCenter;
        label.text = titles[i];
        [headView addSubview:label];
    }
    
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,  CGRectGetMaxY(headView.frame), UIScreenW, UIScreenH -CGRectGetMaxY(headView.frame) -64)];
    tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
    
}

#pragma mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.datas.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CHComplainCountCell *cell = [CHComplainCountCell cellForTableView:tableView];
    if ([self.title isEqualToString:@"评价统计"]) {
        
        cell.pj_model = self.datas[indexPath.row];
    }else{
        cell.ts_model = self.datas[indexPath.row];
    }
    
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.title isEqualToString:@"评价统计"]) {
        id obj = [LongriseRouter openURL:@"CHEvaluationListController"];
        if(obj && [obj isKindOfClass:[UIViewController class]])
        {
            UIViewController *vc = obj;
            [self.navigationController pushViewController:vc animated:YES];
        }
        
        //判断该类是否可以传参数
        if([LongriseRouter isImplLongriseRouterDelegaterWithObject:obj])
        {
            id<LongriseRouterDelegater> delegater = obj;
            if(delegater)
            {
                
                NSMutableDictionary *params = [NSMutableDictionary dictionary];
                params[@"model"] = self.datas[indexPath.row];
                params[@"starttime"] = self.startTime;
                params[@"endtime"] = self.endTime;
                //进行传值，传入统一的block以供B进行回调
                [delegater setParams:params block:^(id obj) {
                    
                    
                }];
            }
        }
    }else{

        
        id obj = [LongriseRouter openURL:@"CHComplaintListController"];
        if(obj && [obj isKindOfClass:[UIViewController class]])
        {
            UIViewController *vc = obj;
            [self.navigationController pushViewController:vc animated:YES];
        }
        
        //判断该类是否可以传参数
        if([LongriseRouter isImplLongriseRouterDelegaterWithObject:obj])
        {
            id<LongriseRouterDelegater> delegater = obj;
            if(delegater)
            {
                
                NSMutableDictionary *params = [NSMutableDictionary dictionary];
                
                TS_Model *model = self.datas[indexPath.row];
                params[@"areaid"] =  model.areaid;
                params[@"areacn"] = model.areacn;
                params[@"starttime"] = self.startTime;
                params[@"endtime"] = self.endTime;
                //进行传值，传入统一的block以供B进行回调
                [delegater setParams:params block:^(id obj) {
                    
                    
                }];
            }
        }
    }
}


/**
 *  显示完全分割线
 */
-(void)viewDidLayoutSubviews
{
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}


#pragma mark LongriseRouterDelegater代理回调方法
-(void)setParams:(id)obj block:(LongriseRouterBlock)block
{
    
    if (obj && [obj isKindOfClass:[NSMutableDictionary class]]) {
    
        NSMutableDictionary *dict = obj;
        self.title = dict[@"title"];
        self.startTime = dict[@"starttime"];
        self.endTime = dict[@"endtime"];
        self.areacn = dict[@"areacn"];
    }
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
